package myzd.services.impl;

import libedge.domain.exceptions.GenericException;
import lombok.extern.slf4j.Slf4j;
import myzd.api.domain.ListResult;
import myzd.api.domain.PagedResult;
import myzd.api.domain.TreeResult;
import myzd.client.DoctorKbClient;
import myzd.domain.enums.ServiceTypeList;
import myzd.domain.response.*;
import myzd.services.ClientService;
import myzd.services.DoctorKbService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * service for doctorKb class
 *
 * @author jimaozou
 * @date 2017/10/21
 */
@Component
@Slf4j
public class DefaultDoctorKbServiceImpl implements DoctorKbService {
  @Autowired
  private ClientService clientService;
  @Autowired
  private DoctorKbClient doctorKbClient;

  @Override
  public PagedResult<Hospital> searchHospitalByName(String hospitalName, Integer page, Integer pageSize) throws GenericException {
    return clientService.getHospitalListByName(hospitalName, page, pageSize);
  }

  @Override
  public PagedResult<Disease> searchDiseaseByDiseaseName(String diseaseName, Integer page, Integer pageSize) throws GenericException {
    return clientService.getDiseaseListByDiseaseName(diseaseName, page, pageSize);
  }

  @Override
  public PagedResult<DoctorDetailsDto> searchDoctors(
    String name, String hospitalName, String diseaseName, Integer parentCategoryId, Integer categoryId, Integer cityId,
    ServiceTypeList serviceType, Integer page, Integer pageSize
  ) throws GenericException {
    return clientService.getDoctorList(name, hospitalName, diseaseName, parentCategoryId, categoryId, cityId, serviceType, page, pageSize);
  }

  @Override
  public PagedResult<City> searchCity(Integer provinceId, String cityName, Integer isCapitalOrder) throws GenericException {
    PagedResult<City> cityList = clientService.getCityList(provinceId, cityName);
    ;
    if (isCapitalOrder != null && isCapitalOrder.equals(1)) {
      cityList.getList().sort(Comparator.comparing(City::getIsCapital).reversed());
    }
    return cityList;
  }

  @Override
  public PagedResult<Province> searchProvinceList(String initial) throws GenericException {
    return clientService.getProvinceList(initial);
  }

  @Override
  public PagedResult<Provinces> searchProvinceListByDesignatedCity(String initial) throws GenericException {
    PagedResult<Province> provincesList = clientService.getProvinceList(initial);
    if (provincesList != null && provincesList.getList() != null && provincesList.getList().size() > 0) {
      return this.getProvincesByMajorCitiesOrder(provincesList.getList());
    } else {
      PagedResult<Provinces> provincesPagedResult = new PagedResult<Provinces>();
      provincesPagedResult.setList(new ArrayList<>());
      return provincesPagedResult;
    }
  }

  //按主要城市排序省份
  private PagedResult<Provinces> getProvincesByMajorCitiesOrder(List<Province> provincesList) {
    PagedResult<Provinces> provincesPagedResult = new PagedResult<Provinces>();
    List<Integer> majorCitiesList = Arrays.asList(110000, 310000, 120000, 500000);
    List<Provinces> headerProvincesList = new ArrayList<>();
    List<Provinces> ProvincesNewList = new ArrayList<>();
    for (int i = 0; i < provincesList.size(); i++) {
      Province provinceInfo = provincesList.get(i);
      Provinces provincesInfo = new Provinces();
      BeanUtils.copyProperties(provinceInfo, provincesInfo);
      //如果北京 上海 天津 重庆
      if (majorCitiesList.contains(provincesInfo.getId())) {
        int indexRanking = majorCitiesList.indexOf(provincesInfo.getId()) + 1;
        provincesInfo.setCityRanking(indexRanking);
        headerProvincesList.add(provincesInfo);
        provincesList.remove(i);
        i--;
      } else {
        provincesInfo.setCityRanking(999);
        ProvincesNewList.add(provincesInfo);
      }
    }
    if (headerProvincesList.size() > 0) {
      headerProvincesList.sort(Comparator.comparing(Provinces::getCityRanking));
    }
    ProvincesNewList.sort(Comparator.comparing(Provinces::getInitial));
    Integer t = 0;
    for (Provinces headerProvincesInfo : headerProvincesList) {
      ProvincesNewList.add(t, headerProvincesInfo);
      t++;
    }
    provincesPagedResult.setList(ProvincesNewList);
    return provincesPagedResult;
  }

  @Override
  public ListResult<TreeResult<DiseaseCategory>> searchAllDiseaseCategory() throws GenericException {
    return clientService.getAllDiseaseCategory();
  }

  @Override
  public PagedResult<Counties> searchCounties(Integer cityId) throws GenericException {
    return clientService.getCountryByCityId(cityId);
  }

}
